<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
date_default_timezone_set('Asia/Manila');
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Queue_Slip extends Page
{
       public $imgBack = null;
       public $memoRemarks = null;
       public $cmbProd = null;
       public $Label44 = null;
       public $Label43 = null;
       public $cmbReturnPlantMin = null;
       public $cmbReturnPlantHr = null;
       public $cmbDeptClientMin = null;
       public $cmbDeptClientHr = null;
       public $cmbActLoadingMin = null;
       public $cmbActLoadingHr = null;
       public $cmbArrivalClientMin = null;
       public $cmbArrivalClientHr = null;
       public $cmbEstReturnMin = null;
       public $cmbEstReturnHr = null;
       public $cmbDeptPlantMin = null;
       public $cmbDeptPlantHr = null;
       public $cmbLoadConfMin = null;
       public $cmbLoadConfHr = null;
       public $cmbIssuanceMin = null;
       public $cmbIssuanceHr = null;
       public $Label42 = null;
       public $Label41 = null;
       public $Label40 = null;
       public $Label39 = null;
       public $Label38 = null;
       public $Label37 = null;
       public $Label36 = null;
       public $Label35 = null;
       public $Label34 = null;
       public $cmbArrPlantMin = null;
       public $cmbArrPlantHr = null;
       public $edHname = null;
       public $Label33 = null;
       public $Label32 = null;
       public $Label31 = null;
       public $cmbRtype = null;
       public $Label29 = null;
       public $cmbReason = null;
       public $edHitMiss = null;
       public $Label30 = null;
       public $dpIsuueLoad = null;
       public $Label22 = null;
       public $Label15 = null;
       public $edVolume = null;
       public $Label28 = null;
       public $edLSite = null;
       public $Label27 = null;
       public $JTJavaScript4 = null;
       public $edFname = null;
       public $Label26 = null;
       public $edLname = null;
       public $Label25 = null;
       public $JSRefreshHaulerList = null;
       public $Image4 = null;
       public $JSRefreshDriverList = null;
       public $imgAddDriver = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $edHtype = null;
       public $cmbHauler = null;
       public $edTruckType = null;
       public $hfHelper = null;
       public $hfInitHelpers = null;
       public $listBoxHelpers = null;
       public $Label23 = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $dpReturnPlant = null;
       public $dpDepartClient = null;
       public $dpActualUnload = null;
       public $dpArrivalClient = null;
       public $dpEstReturn = null;
       public $dpDepPlant = null;
       public $dpLoadConfirmed = null;
       public $dpArrivalPlant = null;
       public $edCustLoc = null;
       public $edCustno = null;
       public $cmbCustname = null;
       public $edTrip = null;
       public $cmbDriver = null;
       public $cmbPlate = null;
       public $edSO = null;
       public $edQueue = null;
       public $edSequence = null;
       public $edDR = null;
       public $Image3 = null;
       public $Label21 = null;
       public $Label20 = null;
       public $Label19 = null;
       public $Label18 = null;
       public $JTSiteTheme1 = null;
       public $Label17 = null;
       public $Label16 = null;
       public $Label14 = null;
       public $Label13 = null;
       public $Label12 = null;
       public $Label11 = null;
       public $Label10 = null;
       public $Image1 = null;
       public $Label9 = null;
       public $Label8 = null;
       public $Label7 = null;
       public $Label6 = null;
       public $Label5 = null;
       public $Label4 = null;
       public $Label3 = null;
       public $Label2 = null;
       public $Label1 = null;
       public $Image2 = null;
       public $lblengine = null;
       function imgBackClick($sender, $params)
       {
       redirect('queue_list.php');


       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('queue_list.php');


       }

       function cmbDriverJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       loadDriver();
       return false;
       <?php

       }

       function Image4JSClick($sender, $params)
       {

       ?>
       var win_height = 650;
       var win_width = 875;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);

       window.open("hauler_create_otf.php", 'Create_Hauler', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function imgAddDriverJSClick($sender, $params)
       {

       ?>
       var win_height = 650;
       var win_width = 675;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);

       window.open("personnel_create_otf.php", 'Create_Personnel', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function cmbCustnameJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       loadCust();
       return false;
       <?php

       }

       function cmbHaulerJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       loadHauler();
       return false;
       <?php

       }

       function btnSubmitClick($sender, $params)
       {

       //Vehicle
       $veh_id = $this->cmbPlate->getItemIndex();
       $query_veh = "SELECT * FROM vehicle_tbl WHERE company_id='".$_SESSION['company_id']."' and vehicle_id='".$veh_id."'";
       $result_veh = mysql_query($query_veh) or die("SELECT VEH ".mysql_error());
       $row_veh = mysql_fetch_array($result_veh);

       //Driver
       $emp_id = $this->cmbDriver->getItemIndex();
       $query_driver = "SELECT * FROM employee_tbl WHERE job_title='Driver' AND company_id='".$_SESSION['company_id']."' and autoid='".$emp_id."'";
       $result_driver = mysql_query($query_driver) or die("SELECT DRIVER ".mysql_error());
       $row_emp = mysql_fetch_array($result_driver);

       //Hauler
       $hauler_id = $this->cmbHauler->getItemIndex();
       $query_hauler = "SELECT * FROM hauler_tbl WHERE company_id='".$_SESSION['company_id']."' and hauler_id='".$hauler_id."'";
       $result_hauler = mysql_query($query_hauler) or die("SELECT HAULER ".mysql_error());
       $row_hauler = mysql_fetch_array($result_hauler);

       //Customer
       $cust_id = $this->cmbCustname->getItemIndex();
       $query_cust = "SELECT * FROM vendor_tbl WHERE company_id='".$_SESSION['company_id']."' and vendor_id='".$cust_id."'";
       $result_cust = mysql_query($query_cust) or die("SELECT CUST ".mysql_error());
       $row_cust = mysql_fetch_array($result_cust);

       //time
       $arr_plant_time = $this->cmbArrPlantHr->getItemIndex().":".$this->cmbArrPlantMin->getItemIndex();
       $issuance_time = $this->cmbIssuanceHr->getItemIndex().":".$this->cmbIssuanceMin->getItemIndex();
       $load_conf_time = $this->cmbLoadConfHr->getItemIndex().":".$this->cmbLoadConfMin->getItemIndex();
       $dept_plant_time = $this->cmbDeptPlantHr->getItemIndex().":".$this->cmbDeptPlantMin->getItemIndex();
       $est_return_time = $this->cmbEstReturnHr->getItemIndex().":".$this->cmbEstReturnMin->getItemIndex();

       $arr_client_time = $this->cmbArrivalClientHr->getItemIndex().":".$this->cmbArrivalClientMin->getItemIndex();
       $act_load_time = $this->cmbActLoadingHr->getItemIndex().":".$this->cmbActLoadingMin->getItemIndex();
       $dept_client_time = $this->cmbDeptClientHr->getItemIndex().":".$this->cmbDeptClientMin->getItemIndex();
       $return_plant_time = $this->cmbReturnPlantHr->getItemIndex().":".$this->cmbReturnPlantMin->getItemIndex();

       //$curr_timestamp = date("Y-m-d H:i:s");
       $query_ins = "insert into queue_tbl (company_id,
       dr_no,
       sequence_no,
       queue_no,
       so_no,
       plate_no,
       plate_id,
       trip_no,
       driver_code,
       driver_name,
       driver_id,
       truck_type,
       hauler_code,
       hauler_name,
       hauler_id,
       hauler_type,
       cust_name,
       cust_id,
       cust_no,
       cust_loc,
       cust_vol,
       cust_prod,
       loading_site,
       hit_miss,
       reason,
       reason_type,
       arrival_at_plant,
       issue_loading_note,
       load_confirmed,
       dept_on_plant,
       est_return,
       arrival_at_client,
       actual_unloading,
       departure_at_client,
       return_to_plant,
       arrival_at_plant_time,
       issue_loading_note_time,
       load_confirmed_time,
       dept_on_plant_time,
       est_return_time,
       arrival_at_client_time,
       actual_unloading_time,
       departure_at_client_time,
       return_to_plant_time,
       helpers_id,
       remarks)

       values ('".$_SESSION['company_id']."',
       '".$this->edDR->Text."',
       '".$this->edSequence->Text."',
       '".$this->edQueue->Text."',
       '".$this->edSO->Text."',
       '".$row_veh['veh_num']."',
       '".$this->cmbPlate->getItemIndex()."',
       '".$this->edTrip->Text."',
       '".$row_emp['emp_id']."',
       '".$row_emp['lname'].','.$row_emp['fname']."',
       '".$this->cmbDriver->getItemIndex()."',
       '".$this->edTruckType->Text."',
       '".$row_hauler['hauler_code']."',
       '".$row_hauler['hauler_name']."',
       '".$this->cmbHauler->getItemIndex()."',
       '".$this->edHtype->Text."',
       '".$row_cust['vendor_name']."',
       '".$this->cmbCustname->getItemIndex()."',
       '".$this->edCustno->Text."',
       '".$this->edCustLoc->Text."',
       '".$this->edVolume->Text."',
       '".$this->cmbProd->getItemIndex()."',
       '".$this->edLSite->Text."',
       '".$this->edHitMiss->Text."',
       '".$this->cmbReason->getItemIndex()."',
       '".$this->cmbRtype->getItemIndex()."',
       '".$this->dpArrivalPlant->Text."',
       '".$this->dpIsuueLoad->Text."',
       '".$this->dpLoadConfirmed->Text."',
       '".$this->dpDepPlant->Text."',
       '".$this->dpEstReturn->Text."',
       '".$this->dpArrivalClient->Text."',
       '".$this->dpActualUnload->Text."',
       '".$this->dpDepartClient->Text."',
       '".$this->dpReturnPlant->Text."',
       '".$arr_plant_time."',
       '".$issuance_time."',
       '".$load_conf_time."',
       '".$dept_plant_time."',
       '".$est_return_time."',
       '".$arr_client_time."',
       '".$act_load_time."',
       '".$dept_client_time."',
       '".$return_plant_time."',
       '".$this->hfHelper->getValue()."',
       '".$this->memoRemarks->Text."')";
       //die($query_ins);
       $result = mysql_query($query_ins)or die("INSERT QUEUE - " . mysql_error());

       $query_sel = "SELECT last_insert_id(autoid) as poiuy FROM queue_tbl WHERE company_id='".$_SESSION['company_id']."' order by autoid DESC limit 0,1";
       $result_sel = mysql_query($query_sel) or die("SELECT QUEUE ".mysql_error());
       $row_sel = mysql_fetch_array($result_sel);


       echo"<script language=javascript>
       alert('Successfully added record!');
       //parent.document.getElementById('flag').value = 'imgStatus';
       //parent.document.images['imgStatus'].src='image/status3.png'
       //parent.clear('imgStatus');
       parent.document.getElementById('JTFrameMain').src = 'queue_list.php';
       var win_height = 600;
       var win_width = 810;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open('print_queue_slip.php?queue_id=".$row_sel['poiuy']."', 'print_inline', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       </script>";

       //redirect('queue_list.php');
       }


       function cmbPlateJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       loadXMLDoc();
       return false;
       <?php

       }


       function clear()
       {
       $this->edDR->Text='';
       $this->edSequence->Text='';
       $this->edQueue->Text='';
       $this->edSO->Text='';
       $this->edTrip->Text='';
       $this->edTruckType->Text='';
       $this->edHtype->Text='';
       $this->edCustno->Text='';
       $this->edCustLoc->Text='';
       $this->dpArrivalPlant->Text='';
       $this->dpLoadConfirmed->Text='';
       $this->dpDepPlant->Text='';
       $this->dpEstReturn->Text='';
       $this->dpArrivalClient->Text='';
       $this->dpActualUnload->Text='';
       $this->dpDepartClient->Text='';
       $this->dpReturnPlant->Text='';
       $this->edLSite->Text='';
       $this->edVolume->Text='';
       $this->edHitMiss->Text='';
       $this->memoRemarks->Lines=array();
       }


       function Queue_SlipCreate($sender, $params)
       {

       $this->clear();

       //die(date_default_timezone_get());

       //auto queue_no
       $result_queue_count = mysql_query("SELECT * FROM queue_tbl WHERE company_id='".$_SESSION['company_id']."'")
       or die("queue count ".mysql_error());
       $row_queue_count = mysql_fetch_array($result_queue_count);

       if(mysql_num_rows($result_queue_count)==0)
       {
        $this->edQueue->Text = '000001';
       }
       if(mysql_num_rows($result_queue_count)>0)
       {

        $count = mysql_num_rows($result_queue_count);
        $number = $count+1;

        $length = strlen($number);
        $num_zero = 6 - $length;
        $zero = '';
        while($num_zero>0)
         {
           $zero = $zero.'0';
           $num_zero--;
         }
        $this->edQueue->Text = $zero.$number;
        }


       //Driver drop-down
       $query_driver = "SELECT * FROM employee_tbl WHERE job_title='Driver' AND company_id='".$_SESSION['company_id']."'";
       $result_driver = mysql_query($query_driver) or die("SELECT DRIVER ".mysql_error());
       $this->cmbDriver->Clear();
       $this->cmbDriver->AddItem("--Select--",'',-1);
       $this->cmbDriver->setItemIndex('--Select--');
       while($row_driver = mysql_fetch_array($result_driver))
       {
        $this->cmbDriver->AddItem($row_driver['emp_id'],null,$row_driver['autoid']);
       }


       //Vehicle drop-down
       $query_veh = "SELECT * FROM dts_veh_tbl WHERE company_id='".$_SESSION['company_id']."'";
       $result_veh = mysql_query($query_veh) or die("SELECT VEH ".mysql_error());
       //die($query_veh);
       $this->cmbPlate->Clear();
       $this->cmbPlate->AddItem("--Select--",'',-1);
       $this->cmbPlate->setItemIndex('--Select--');
       while($row_veh = mysql_fetch_array($result_veh))
       {
        $ctr_clear = 0;
        $query_check_queue = "SELECT status FROM queue_tbl WHERE plate_id='".$row_veh['vehicle_id']."'";
        $result_check_queue = mysql_query($query_check_queue) or die("SELECT CHECK QUEUE ".mysql_error());
        while($row_check_queue = mysql_fetch_array($result_check_queue))
        {
         if($row_check_queue['status']!='complete')
         {
         $ctr_clear++;
         }
        }
        if($ctr_clear==0)
        $this->cmbPlate->AddItem($row_veh['veh_num'],null,$row_veh['vehicle_id']);
       }


       //Hauler drop-down
       $query_hauler = "SELECT * FROM hauler_tbl WHERE company_id='".$_SESSION['company_id']."'";
       $result_hauler = mysql_query($query_hauler) or die("SELECT HAULER ".mysql_error());
       //die($query_veh);
       $this->cmbHauler->Clear();
       $this->cmbHauler->AddItem("--Select--",'',-1);
       $this->cmbHauler->setItemIndex('--Select--');
       while($row_hauler = mysql_fetch_array($result_hauler))
       {
        $this->cmbHauler->AddItem($row_hauler['hauler_code'],null,$row_hauler['hauler_id']);
       }


       //Customer drop-down
       $query_vendor = "SELECT * FROM vendor_tbl WHERE company_id='".$_SESSION['company_id']."' and isCustomer='1'";
       $result_vendor = mysql_query($query_vendor) or die("SELECT VENDOR ".mysql_error());
       //die($query_veh);
       $this->cmbCustname->Clear();
       $this->cmbCustname->AddItem("--Select--",'',-1);
       $this->cmbCustname->setItemIndex('--Select--');
       while($row_vendor = mysql_fetch_array($result_vendor))
       {
        $this->cmbCustname->AddItem($row_vendor['vendor_name'],null,$row_vendor['vendor_id']);
       }

       //Hr/Min Est Arrival Plant drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbArrPlantHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbArrPlantMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Issuance of loading drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbIssuanceHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbIssuanceMin->AddItem($min,'',$min);
       $count++;
       }



       //Hr/Min Load Confirmed drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbLoadConfHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbLoadConfMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Dept Plant drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbDeptPlantHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbDeptPlantMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Est Return drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbEstReturnHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbEstReturnMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Arrival Client drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbArrivalClientHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbArrivalClientMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Actual loading drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbActLoadingHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbActLoadingMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Departure on client drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbDeptClientHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbDeptClientMin->AddItem($min,'',$min);
       $count++;
       }


       //Hr/Min Return Plant drop-down
       $count = 0;
       while($count<=23)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $hr = '0'.$count;
       }
       else
       {
       $hr = $count;
       }
       $this->cmbReturnPlantHr->AddItem($hr,'',$hr);
       $count++;
       }

       $count = 0;
       while($count<=59)
       {
       $exp_count = strlen($count);
       if($exp_count==1)
       {
       $min = '0'.$count;
       }
       else
       {
       $min = $count;
       }
       $this->cmbReturnPlantMin->AddItem($min,'',$min);
       $count++;
       }


       }



       function listBoxHelpersJSClick($sender, $params)
       {

       ?>
       var win_height = 550;
       var win_width = 610;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);

       hids = document.getElementById("hfInitHelpers").value;
       window.open("select_helper.php?hids="+hids, 'Select_Trouble', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

}

global $application;

global $Queue_Slip;

//Creates the form
$Queue_Slip=new Queue_Slip($application);

//Read from resource file
$Queue_Slip->loadResource(__FILE__);

//Shows the form
$Queue_Slip->show();

?>